package com.springboot.blog.controller.admin;

import com.springboot.blog.dto.LayPage;
import com.springboot.blog.dto.LayResponse;
import com.springboot.blog.entity.Pic;
import com.springboot.blog.interfaces.ToolInterface;
import com.springboot.blog.service.IPicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 七牛云图片上传列表
 */
@Controller
class AdminPicController {
    @Autowired
    IPicService picService;
    String PATH = "admin/pic/";

    @RequiresPermissions("pic:list")
    @GetMapping(value = {"/admin/pic/index", "/admin/pic"})
    public String index() {
        return PATH + "index";
    }

    @RequiresPermissions("pic:list")
    @GetMapping(value = {"/admin/pic/tree"})
    @ResponseBody
    public LayResponse tree(Integer page, Integer limit) {
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        LayPage<Pic> pagePic = new LayPage<>(page, limit);
        picService.page(pagePic);
        return pagePic.getLayResponse();
    }

    @RequiresPermissions("pic:delete")
    @PostMapping(value = {"/admin/pic/delete"})
    @ResponseBody
    public LayResponse delete(String id) {
        boolean result = picService.removeById(id);
        LayResponse layResponse = new LayResponse();
        if (result) {
            return layResponse.success();
        }
        return layResponse.error();
    }

    @RequiresPermissions("pic:delete")
    @PostMapping(value = {"/admin/pic/deleteAll"})
    @ResponseBody
    public LayResponse deleteAll(@RequestParam("ids[]") List<Integer> ids) {
        boolean result = picService.removeByIds(ids);
        LayResponse layResponse = new LayResponse();
        if (result) {
            return layResponse.success();
        }
        return layResponse.error();
    }

    @RequiresPermissions("pic:add")
    @GetMapping(value = {"/admin/pic/add"})
    public String add(Model model) {
        return PATH + "add";
    }

    @RequiresPermissions("pic:add")
    @PostMapping(value = {"/admin/pic/add"})
    @ResponseBody
    public LayResponse add(@Validated(ToolInterface.insert.class) Pic pic) {
        picService.save(pic);
        return new LayResponse().success();
    }

    @RequiresPermissions("pic:edit")
    @GetMapping(value = {"/admin/pic/edit"})
    public String edit(@RequestParam String id,Model model) {
        Pic data = picService.getById(id);
        model.addAttribute("data",data);
        return PATH + "edit";
    }

    @RequiresPermissions("pic:edit")
    @PostMapping(value = {"/admin/pic/edit"})
    @ResponseBody
    public LayResponse edit(@Validated(ToolInterface.update.class) Pic pic) {
        picService.updateById(pic);
        return new LayResponse().success();
    }
}
